package com.example.wacoffee_manage.Mapper;

import com.example.wacoffee_manage.DTO.getLastDTO;
import com.example.wacoffee_manage.annotation.AutoFill;
import com.example.wacoffee_manage.common.OperationType;
import com.example.wacoffee_manage.pojo.Good;
import com.example.wacoffee_manage.pojo.Order;
import com.example.wacoffee_manage.pojo.item;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {

    @AutoFill(OperationType.INSERT)
    void addOrder(Order order);


    @Select("select sum(expend) from orders where date(create_time)=date(now()) and status=3")
    Double getTodayRevenue();
    @Select("select count(id) from orders where date(create_time)=date(now()) and status in(1,3)")
    Integer getTotalOrders();
    @Select("select count(id) from orders where date(create_time)=date(now()) and status=3")
    Integer getCompletedOrders();

    @Select("select id, information, expend, create_time, creator, status, user_id from orders where status=1")
    List<Order> getUnfinishedOrder();

    void updateStock(item[] items);

    @Update("update orders set status=3 where id=#{id}")
    void updateOrder(Integer id);

    void addOrderGood(@Param("order") Order order);

    @Select("select g.id as id,og.consume as stock from order_good og left join good g on og.good_id = g.id where og.order_id=#{id}" )
    List<Good> getGoodsByOrderId(int id);

    void updateGoodStock(List<Good> goods);

    @Update("update orders set status=2 where id= #{id}")
    void updateOrderStatus2(int id);

    @Select("select id, information, expend, create_time, creator, status, user_id from orders where id=#{orderId}")
    Order getById(int orderId);

    @Delete("delete from orders where status=0 and create_time < #{standard}")
    void processTimeoutOrder(LocalDateTime standard);
    @Select("select id from orders where status=0  and create_time < #{standard}")
    Integer[] getProcessTimeoutOrderIds(LocalDateTime standard);

    void processTimeoutOrderInOrderGood(Integer[] ids);

    @Select("select count(distinct good_id) as counts " +
            "from order_good og " +
            "left join orders o " +
            "on og.order_id=o.id " +
            "where date(o.create_time) between #{startDate} and #{endDate}")
    int getTotalGoods(getLastDTO getLastDTO);

    @Update("update user set balance=balance+#{expend} where id=#{userId}")
    void addBalance(Integer userId, double expend);
}
